package com.xiaoyu.client.ui.fragment.help;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.xiaoyu.client.ClientApplication;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.commonlib.ui.adapter.AllPowerfulAdapter;


import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author jx
 * @date 创建时间 2018/10/26
 * @Description 求助 -- 我的求助  --  我发起的
 */
public class MySeekFragment extends Fragment implements SeekModel.ISeekListListener {

    private static final String ARG_PARAM = "param";
    private String mParam;
    private View mView;
    @BindView(R.id.common_recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.activity_my_help_initiate)
    LinearLayout mInitiateLinear;
    @BindView(R.id.my_help_item_recyclerview_radiogroup)
    RadioGroup mRadioGroup;
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.help_item_empty)
    ImageView mEmptyImg;


    public MySeekFragment newInstance(String param) {
        MySeekFragment fragment = new MySeekFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM, param);
        fragment.setArguments(args);
        return fragment;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.my_help_item_recyclerview, container, false);
            ButterKnife.bind(this, mView);
            initView();
        }
        return mView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam = getArguments().getString(ARG_PARAM);
        }
    }

    @SuppressLint("ResourceAsColor")
    private void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setBackgroundColor(getResources().getColor(R.color.color_gray_F3));


        //上拉下拉刷新设置
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsHeader(context);
            }
        });
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
        setParamsItem();
    }


    //判断是点击的哪个
    private void setParamsItem() {
        if (mParam.equals("help0")) {//我发起的
            mInitiateLinear.setVisibility(View.VISIBLE);
            new SeekModel(0, -1, mRefreshLayout, getContext(),mEmptyImg).setListListener(MySeekFragment.this);
            mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {
                    switch (radioGroup.getCheckedRadioButtonId()) {
                        case R.id.all_radiobutton:
                            new SeekModel(0, -1, mRefreshLayout, getContext(),mEmptyImg).setListListener(MySeekFragment.this);
                            break;
                        case R.id.resolved_radiobutton:
                            new SeekModel(0, 1, mRefreshLayout, getContext(),mEmptyImg).setListListener(MySeekFragment.this);
                            break;
                        case R.id.unsolved_radiobutton:
                            new SeekModel(0, 0, mRefreshLayout, getContext(),mEmptyImg).setListListener(MySeekFragment.this);
                            break;
                    }
                }
            });
            return;
        }
        if (mParam.equals("help1")) {//我参与的
            new SeekModel(1, 5, mRefreshLayout, getContext(),mEmptyImg).setListListener(this);
            return;
        }
        if (mParam.equals("help2")) {//我收藏的
            new SeekModel(2, 5, mRefreshLayout, getContext(),mEmptyImg).setListListener(this);
            return;
        }
    }

    @Override
    public void seekList(AllPowerfulAdapter mAdapter) {
        mRecyclerView.setAdapter(mAdapter);
    }

}
